
#include <stdio.h>
#include "CH543.H"
#include "DEBUG.H"
#include "GPIO.H"
#include "ADC.H"
#include "PD_PHY.H"
#include "BC_QC_PHY.h"
#include "system.h"



// UINT8X PD_Request_Gears = 1;
// UINT8X Temp_PDO = 3;

// UINT8X Gears_Plus = 0;
// UINT8X Gears_Sub = 0;

void main() {
    UINT8 Status = 0;
    UINT16 adc_value;
    UINT8 cnt;
    BOOL usb_in_flg=0;
    BOOL dc_in_flg=0;
    WORK_MODE_T work_mode;
    BC_TYPE_T bc_type;
    USB_INPUT_CFG usb_in_cfg = 0xff;

    pd_phy_init();  // �����Source��Sink���г�ʼ����
    CfgFsys();      /* 24MHz */
    mInitSTDIO();   /* UART��ʼ�� */
    Timer0Init();
    /* ADC初始化 */
    ADC_Init(0);
        /* IIC从机初始化 */
    I2CS_Slave_Init();
    // mDelaymS(10);
    my_printf(" HeiMaXiaoWuGui PD3.0 Sink V:%d\n", SW_VERSION);

    UPD_CTRL |= bPD_DMA_EN;
    IE_USBPD = 1;
    EA = 1;
    GPIO_Init( PORT3,PIN2,MODE2 );
    P3_2 = 0;

PD_Start:
    Status = 0;
    cnt = 0;
    work_mode = WORK_M_NULL;

    while (1) {
        adc_value = Check_ADC(VBUS_CH);

        if (adc_value >= VBUS_4V) { /*USB口有输入电压*/
            SET_USB_IN_FLG(1)
            if(!usb_in_flg){
                usb_in_flg = 1;
                P3_2 = 0;
            }
            if (usb_in_cfg != get_out_cfg()) {
                usb_in_cfg = get_out_cfg();
                work_mode = WORK_M_NULL;
                my_printf("cfg input\n", 0);
            }
            
            switch (work_mode) {
                case WORK_M_NULL: {
                    Status = Check_CC_Connect();
                    if (Status) {
                        pd_phy_rx_init();
                        work_mode = WORK_M_PD;
                        SET_USB_IN_INFOR(USB_INFOR_PD2_0);
                        P3_2 = 0;
                    } else {
                        cnt++;
                        if (cnt > 70) {
                            work_mode = WORK_M_BC12_OR_QC;
                            bc_type = BC_TYPE_NULL;
                        }
                    }
                } break;

                case WORK_M_BC12_OR_QC:
                    if (bc_type == BC_TYPE_NULL) {
                        bc_type = BC1_2Check();
                    } else {
                        switch (bc_type) {
                            case BC_TYPE_CDP: {  // 充电下游端口CDP
                                SET_USB_IN_INFOR(USB_INFOR_CDP);
                                P3_2 = 0;
                                work_mode = WORK_M_QC_REQ_SUCCCESS;
                            } break;
                            case BC_TYPE_SDP: {  // 标准下游端口SDP
                                SET_USB_IN_INFOR(USB_INFOR_SDP);
                                P3_2 = 0;
                                work_mode = WORK_M_QC_REQ_SUCCCESS;
                            } break;
                            case BC_TYPE_DCP: {  // 专用充电端口DCP
                                work_mode = out_cfg_table_for_QC[get_out_cfg()];
                                SET_USB_IN_INFOR(USB_INFOR_BC1_2);
                            } break;
                            case BC_TYPE_UN_KNOWN:
                                my_printf("Charger Unknown\n", 0);
                                work_mode = WORK_M_UNKNOWN;
                                P3_2 = 0;
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case WORK_M_WAIT_QC_REQ_20V:
                    work_mode = WORK_REQ20V_WAIT;
                    QC_Get_Request20V();
                    cnt = 0;
                    break;
                case WORK_REQ20V_WAIT:
                    cnt++;
                    if (cnt < 25) {
                        // adc_value = Check_ADC(VBUS_CH);
                        if (adc_value > VBUS_18P5V) {
                            work_mode = WORK_M_QC_REQ_SUCCCESS;
                            my_printf("QC-20V\n", 0);
                            P3_2 = 0;
                            SET_USB_IN_INFOR(USB_INFOR_QC2_0);
                        }
                    } else {
                        work_mode = WORK_M_WAIT_QC_REQ_12V;
                    }
                    break;

                case WORK_M_WAIT_QC_REQ_12V:
                    QC_Get_Request12V();
                    cnt = 0;
                    work_mode = WORK_REQ12V_WAIT;
                    break;
                case WORK_REQ12V_WAIT:
                    cnt++;
                    if (cnt < 25) {
                        // adc_value = Check_ADC(VBUS_CH);
                        if (adc_value > VBUS_11V) {
                            work_mode = WORK_M_QC_REQ_SUCCCESS;
                            my_printf("QC-12V\n", 0);
                            P3_2 = 0;
                            SET_USB_IN_INFOR(USB_INFOR_QC2_0);
                        }
                    } else {
                        work_mode = WORK_M_WAIT_QC_REQ_9V;
                    }
                    break;
                case WORK_M_WAIT_QC_REQ_9V:
                    work_mode = WORK_REQ9V_WAIT;
                    QC_Get_Request9V();
                    cnt = 0;
                    break;

                case WORK_REQ9V_WAIT:
                    cnt++;
                    if (cnt < 25) {
                        // adc_value = Check_ADC(VBUS_CH);
                        if (adc_value > VBUS_8V) {
                            work_mode = WORK_M_QC_REQ_SUCCCESS;
                            my_printf("QC-9V\n", 0);
                            P3_2 = 0;
                            SET_USB_IN_INFOR(USB_INFOR_QC2_0);
                        }
                    } else {
                        work_mode = WORK_M_QC_REQ_SUCCCESS;
                        my_printf("QC-5V\n", 0);
                        P3_2 = 0;
                        SET_USB_IN_INFOR(USB_INFOR_QC2_0);
                        QC_Get_Request5V();
                        cnt = 0;
                    }
                    break;
                case WORK_M_PD:

                    break;
                case WORK_M_UNKNOWN:
                    break;
                default:
                    break;
            }

        } else { /*是DC口有输入电压*/
            SET_USB_IN_FLG(0)
            work_mode = WORK_M_NULL;
            cnt = 0;
            if(usb_in_flg){
                usb_in_flg = 0;
                P3_2 = 0;
            }
            // SET_USB_IN_INFOR(USB_INFOR_QC2_0);
        }

        adc_value = Check_ADC(DC_IN_CH);

        if (adc_value > VBUS_4V) {
            SET_DC_IN_FLG(1);
            if(!dc_in_flg){
                dc_in_flg = 1;
                P3_2 = 0;
            }
        } else {
            SET_DC_IN_FLG(0);
            if(dc_in_flg){
                dc_in_flg = 0;
                P3_2 = 0;
            }
        }

        if ((i2cs_flag == 1) && (i2cs_recv_flag == 1)) {
            my_printf("i2c operation\n", 0);
        }
        mDelaymS(20);
    }
}
